<?php

namespace api\modules\v1\forms\clinic;

use Yii;
use yii\base\Model;
use common\models\clinic\Doctor;
use common\models\clinic\Clinic;

/**
 * 医生
 *
 * Class DoctorForm
 * @package api\modules\v1\forms\clinic
 */
class DoctorForm extends Model
{
    // 医生ID
    public $id;
    // 诊室ID
    public $clinic_id;
    // 医生姓名
    public $name;
    // 状态
    public $status;

    // 诊室信息
    protected $_clinicInfo;
    // 医生信息
    protected $_doctorInfo;


    public function rules()
    {
        return [
            // 参数验证
            [['name', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['id', 'name', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],

            [['id'], 'checkId', 'skipOnEmpty' => false, 'on' => ['edit']],
            [['name'], 'checkName', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['clinic_id'], 'checkClinicId', 'skipOnEmpty' => true, 'on' => ['create','edit']],
            [['status'], 'checkStatus', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['clinic_id', 'name', 'status'];

        // 编辑
        $scenarios['edit'] = ['id', 'clinic_id', 'name', 'status'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => '医生ID',
            'clinic_id' => '诊室ID',
            'name' => '医生姓名',
            'status' => '状态',
        ];
    }

    /**
     * 检验id
     *
     * @param $attribute
     * @return bool
     */
    public function checkId($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $doctor = $this->getDoctorInfo();
        if (empty($doctor)) {
            $this->addError($attribute, '医生不存在');
            return true;
        }

        if ($doctor->project_id != Yii::$app->params['project_id']) {
            $this->addError($attribute, '医生非当前项目');
            return true;
        }
    }

    /**
     * 检验诊室ID
     *
     * @param $attribute
     * @return bool
     */
    public function checkClinicId($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (empty($this->clinic_id)) {
            return true;
        }

        $clinic = $this->getClinicInfo();
        if (empty($clinic)) {
            $this->addError($attribute, '诊室不存在');
            return true;
        }

        if ($clinic->status != Clinic::STATUS_ENABLE) {
            $this->addError($attribute, '诊室未启用');
            return true;
        }

        $isCheck = 1;
        if ($this->getScenario() == 'edit') {
            $doctor = $this->getDoctorInfo();
            if ($doctor->clinic_id == $clinic->id
                && ($doctor->status == $this->status || ($doctor->status != $this->status && $this->status == Doctor::STATUS_INVALID))) {
                $isCheck = 0;
            }
        }

        if ($isCheck) {
            $other = Doctor::findOne([
                'project_id' => Yii::$app->params['project_id'],
                'clinic_id' => $clinic->id,
                'status' => Doctor::STATUS_ENABLE,
            ]);
            if ($other) {
                $this->addError($attribute, '诊室已绑定医生');
                return true;
            }
        }
    }

    /**
     * 校验医生姓名
     *
     * @param $attribute
     */
    public function checkName($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $len = mb_strlen($this->name);
        if ($len > 16) {
            $this->addError($attribute, '医生姓名过长[1-16字节]');
            return true;
        }
    }

    /**
     * 校验诊室状态
     *
     * @param $attribute
     */
    public function checkStatus($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (!in_array($this->status, [Doctor::STATUS_INVALID, Doctor::STATUS_ENABLE])) {
            $this->addError($attribute, "非法参数");
            return true;
        }
    }

    /**
     * 获取医生信息
     *
     * @return Doctor|null
     */
    public function getDoctorInfo()
    {
        if (is_null($this->_doctorInfo)) {
            $this->_doctorInfo = Doctor::findOne($this->id);
        }

        return $this->_doctorInfo;
    }

    /**
     * 获取诊室信息
     *
     * @return Clinic|null
     */
    public function getClinicInfo()
    {
        if (is_null($this->_clinicInfo)) {
            $this->_clinicInfo = Clinic::findOne($this->clinic_id);
        }

        return $this->_clinicInfo;
    }
}
